<?php
/**
 * @category Goatherd
 * @package Goatherd\Commons
 *
 * @author Copyright (c) 2012 Maik Penz <maik@phpkuh.de>
 * @version $Id: EntityTrait.php 84 2012-03-27 22:24:33Z maik@phpkuh.de $
 *
 * This file is part of Goatherd library.
 *
 * Goatherd library is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * Goatherd library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Goatherd library. If not, see <http://www.gnu.org/licenses/>.
 */
namespace Goatherd\Commons\Collection;

/**
 *
 * @category Goatherd
 * @package Goatherd\Commons
 * @subpackage Entity
 */
trait EntityTrait
{
    /**
     * Entity of key-value-pairs.
     *
     * @var array
     */
    protected $_properties = array();

    /**
     * Whether to allow adding of new key.
     * @todo is naming suitable?
     *
     * @var boolean
     */
    protected $_writable = false;

    /**
     * (non-PHPdoc)
     * @see Goatherd\Commons\Collection.IEntity::__get()
     */
    public function __get($key)
    {
        $method = 'get'.ucfirst($key);
        if (method_exists($this, $method)) {
            return $this->$method();
        }
        if (!array_key_exists($key, $this->_properties)) {
            throw new \OutOfBoundsException(sprintf("Unknown key '%s'.", $key));
        }

        return $this->_properties[$key];
    }

    /**
     * (non-PHPdoc)
     * @see Goatherd\Commons\Collection.IEntity::__set()
     */
    public function __set($key, $value)
    {
        $method = 'set'.ucfirst($key);
        if (method_exists($this, $method)) {
            return $this->$method($value);
        }
        if (!$this->_writable && !array_key_exists($key, $this->_properties)) {
            throw new \OutOfBoundsException(sprintf("Unknown key '%s'.", $key));
        }

        $this->_properties[$key] = $value;
    }

    /**
     * (non-PHPdoc)
     * @see Goatherd\Commons\Collection.IEntity::__unset()
     */
    public function __unset($key)
    {
        $method = 'unset'.ucfirst($key);
        if (method_exists($this, $method)) {
            return $this->$method();
        }
        if (!array_key_exists($key, $this->_properties)) {
            throw new \OutOfBoundsException(sprintf("Unknown key '%s'.", $key));
        }

        unset($this->_properties[$key]);
    }

    /**
     * (non-PHPdoc)
     * @see Goatherd\Commons\Collection.IEntity::__isset()
     */
    public function __isset($key)
    {
        $method = 'isset'.ucfirst($key);
        if (method_exists($this, $method)) {
            return $this->$method();
        }

        return array_key_exists($key, $this->_properties);
    }

    /**
     * (non-PHPdoc)
     * @see Goatherd\Commons\Collection.IEntity::isWritable()
     */
    public function isWritable($writable = null)
    {
        $wasWritable = $this->_writable;

        if (is_bool($writable)) {
            $this->_writable = $writable;
        }

        return $wasWritable;
    }

    /**
     * (non-PHPdoc)
     * @see Goatherd\Commons\Collection.IEntity::getProperties()
     */
    public function getProperties()
    {
        return $this->_properties;
    }
}
